package com.mjlf.framework.util;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @auther a123
 * @create 2018-08-25 15:08
 * @desc 使用这个类做配置信息读取，同时后边在使用@Value读取信息的时候也是在这个工具类中查找对应的属性
 *
 * 配置文件路径ConfigUtil.class.getResource("/mjlf.properties")
 */
public final class ConfigUtil {
    public final static Map<String, String> CONFIG_VALUE = new HashMap<>();

    public static void readConfig(String path) {
        Properties properties = new Properties();
        try {
            if(System.getProperty("os.name").toUpperCase().contains("WINDOWS")){
                path = path.substring(1);
            }
            properties.load(Files.newInputStream(Paths.get(path), StandardOpenOption.READ));
            if(!properties.isEmpty()){
                Set keys = properties.keySet();
                keys.stream().forEach(key -> {
                    CONFIG_VALUE.put(key.toString(), properties.getProperty(key.toString()));
                });
            }
        } catch (Exception e) {
            throw new RuntimeException("读取配置文件失败", e);
        }
    }

    public static Object getValue(String key){
        return CONFIG_VALUE.get(key);
    }
}
